﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Financeiro.Model;

namespace Financeiro
{
    public partial class CotaBusca : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                // carrega o dropdown de anos 
                for (int i = DateTime.Now.Year; i >= 2000; i--)
                {
                    ListItem obj = new ListItem();
                    obj.Text = i.ToString();
                    obj.Value = i.ToString();
                    drpAnoStart.Items.Add(obj);
                    drpAnoEnd.Items.Add(obj);
                }

                //carrega o dropdown de meses
                for (int i = 1; i <= 12; i++)
                {
                    ListItem li = new ListItem();
                    li.Value = i.ToString();
                    if (i == 1) li.Text = "Janeiro";
                    else if (i == 2) li.Text = "Fevereiro";
                    else if (i == 3) li.Text = "Março";
                    else if (i == 4) li.Text = "Abril";
                    else if (i == 5) li.Text = "Maio";
                    else if (i == 6) li.Text = "Junho";
                    else if (i == 7) li.Text = "Julho";
                    else if (i == 8) li.Text = "Agosto";
                    else if (i == 9) li.Text = "Setembro";
                    else if (i == 10) li.Text = "Outubro";
                    else if (i == 11) li.Text = "Novembro";
                    else if (i == 12) li.Text = "Dezembro";
                    else break;
                    drpMesEnd.Items.Add(li);
                    drpMesStart.Items.Add(li);
                }

                drpMesStart.SelectedValue = DateTime.Today.Month.ToString();
                drpMesEnd.SelectedValue = DateTime.Today.Month.ToString();
            }
        }


        protected void bntBuscar_Click(object sender, EventArgs e)
        {
            int mesInicio = Convert.ToInt32(drpMesStart.SelectedValue);
            int anoInicio = Convert.ToInt32(drpAnoStart.SelectedValue);
            DateTime inicio = new DateTime(anoInicio, mesInicio, 1);

            int mesFim = Convert.ToInt32(drpMesEnd.SelectedValue);
            int anoFim = Convert.ToInt32(drpAnoEnd.SelectedValue);
            DateTime fim = new DateTime(anoFim, mesFim, 1);
            fim = fim.AddMonths(1).Subtract(TimeSpan.FromSeconds(1));

            if (fim.CompareTo(inicio) > 0)
            {
                string setor = "";
                DataTable dt = CotaModel.obterCotas(inicio, fim, setor);
                atualizarDataGrid(dt);
            }
            else
                DateIntervalValidationMsg.Text = "&nbsp;Selecione um intervalo de data adequado&nbsp;";
        }

        private void atualizarDataGrid(DataTable dt)
        {
            grdCotas.Visible = false;
            
            if (dt.Rows.Count != 0)
            {
                grdCotas.Visible = true;
                grdCotas.DataSource = dt;
                grdCotas.DataBind();
            }
        }

        protected void grdCotas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                e.Row.Attributes.Add("style", "cursor:pointer;");

                DataRowView rowView = (DataRowView)e.Row.DataItem;
                string id = rowView["id"].ToString();
                e.Row.Attributes.Add("onclick", "location='CotaDetalhe.aspx?id=" + id + "'");
            }
        }

    }
}